package cn.wenjie.gd.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.wenjie.gd.domain.Collection;
import cn.wenjie.gd.domain.Medical;
import cn.wenjie.gd.domain.MedicalTop;
import cn.wenjie.gd.service.CollectionService;
import cn.wenjie.gd.service.CollectionServiceImp;
import cn.wenjie.gd.service.MedicalService;
import cn.wenjie.gd.service.MedicalServiceImp;
import cn.wenjie.gd.util.MedicalPage;
import cn.wj.base.annotation.Packing;
import cn.wj.base.annotation.RequestMapping;
import cn.wj.base.controller.BaseController;
import cn.wj.base.util.AdviceImp;
import cn.wj.base.util.ProxyUtil;

@WebServlet("/input")
public class InputController extends BaseController {
	private MedicalService medicalService = ProxyUtil.getProx(new MedicalServiceImp(), new AdviceImp());
	private CollectionService collectionService = ProxyUtil.getProx(new CollectionServiceImp(), new AdviceImp());
	
	@RequestMapping(method = "index")
	public String index(@Packing(name = "page", type = MedicalPage.class) MedicalPage page,HttpServletRequest request, HttpServletResponse response) {
		page.setUrl(request.getRequestURI());
		String strpageSize= request.getParameter("pageSize");
		if(strpageSize!=null && !strpageSize.equals("")) page.setPageSize(Integer.parseInt(strpageSize));
		String strpageNum= request.getParameter("pageNum");
		if(strpageNum!=null && !strpageNum.equals("")) page.setPageNum(Integer.parseInt(strpageNum));
		MedicalPage medicalPage =medicalService.getPageList(page);
		addAttribute("page", medicalPage);
		return "/pages/input/list.jsp";
	}
	
	@RequestMapping(method = "delete",auto=false)
	public String delete(@Packing(name = "id", type = Integer.class) Integer id,HttpServletRequest request, HttpServletResponse response) throws IOException{
		medicalService.delete(id);
		response.sendRedirect(request.getContextPath()+"/input/index.do");
		return null;
	}
	
	@RequestMapping(method = "toUpdate")
	public String toUpdate(@Packing(name = "id", type = Integer.class) Integer id,HttpServletRequest request, HttpServletResponse response){
		Medical medical=medicalService.getById(id);
		addAttribute("medical", medical);
		return "/pages/input/update.jsp";
	}

	@RequestMapping(method = "update",auto=false)
	public String update(@Packing(name = "medical", type = Medical.class) Medical medical,HttpServletRequest request, HttpServletResponse response) throws IOException{
		Medical m=medicalService.getById(medical.getId());
		medical.setNumber(m.getNumber());
		medicalService.update(medical);
		response.sendRedirect(request.getContextPath()+"/input/index.do");
		return null;
	}
	
	@RequestMapping(method = "show")
	public String show(@Packing(name = "id", type = Integer.class) Integer id,HttpServletRequest request, HttpServletResponse response){
		Medical medical=medicalService.getById(id);
		addAttribute("medical", medical);
		return "/pages/input/show.jsp";
	}
	
	@RequestMapping(method="checkNumber",auto=false,isAjax=true)
	public String checkNumber(@Packing(name = "number", type = String.class) String number,HttpServletRequest request, HttpServletResponse response) throws IOException{
		if(medicalService.checkNumber(number)) return "NO";
		return "YES";
	}

	@RequestMapping(method = "toAdd")
	public String toAdd(HttpServletRequest request, HttpServletResponse response) throws IOException{
		return "/pages/input/add.jsp";
	}
	
	@RequestMapping(method = "add",auto=false)
	public String add(@Packing(name = "medical", type = Medical.class) Medical medical,HttpServletRequest request, HttpServletResponse response) throws IOException{
		medicalService.save(medical);
		response.sendRedirect(request.getContextPath()+"/input/index.do");
		return null;
	}
	
	@RequestMapping(method = "select")
	public String select(HttpServletRequest request, HttpServletResponse response) throws IOException{
		List<MedicalTop> medicalList=medicalService.getTodayTop();
		List<Collection> collectionList=collectionService.todayGrade();
		addAttribute("medicalList", medicalList);
		addAttribute("collectionList", collectionList);
		return "/pages/input/select.jsp";
	}
	
	
 
	
}
